package com.example.snowliwork4;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;

import com.example.snowliwork4.adapter.PageAdapter;
import com.example.snowliwork4.fragment.BagFragment;
import com.example.snowliwork4.fragment.FoodFragment;
import com.example.snowliwork4.fragment.InfantFragment;
import com.example.snowliwork4.fragment.RecommendFragment;
import com.example.snowliwork4.fragment.StoreFragment;
import com.example.snowliwork4.fragment.UnderwearFragment;
import com.example.snowliwork4.fragment.WomenwearFragment;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    //标签和视图
    //声明tab标签和ViewPager2
    private TabLayout tab1;
    private ViewPager2 vp2;

    //声明子页面的数据源，使用集合存储
    private List<Fragment> fragments;
    //声明子页面适配器
    private PageAdapter pageAdapter;

    //设置购物车列表数据
    public static List<Shop> shoCart = new ArrayList<>();
    //购物车每个商品的购买数量
    public static List<Integer> curnum = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //获取控件对象
        getViews();


        //初始化子页面数据资源
        initPages();


        //初始化子页面适配器对象
        pageAdapter = new PageAdapter(fragments,this);
        //设置选项卡切换时滑动方向
        vp2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
        //给viewPage2绑定适配器
        vp2.setAdapter(pageAdapter);
        //关联ViewPager2和Tablayout
        TabLayoutMediator mediator = new TabLayoutMediator(
                tab1,
                vp2,
                new TabLayoutMediator.TabConfigurationStrategy() {
                    @Override
                    public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                        //设置选型卡文本
                        switch (position){
                            case 0:
                                tab.setText("推荐");
                                break;
                            case 1:
                                tab.setText("母婴");
                                break;
                            case 2:
                                tab.setText("百货");
                                break;
                            case 3:
                                tab.setText("鞋包");
                                break;
                            case 4:
                                tab.setText("食品");
                                break;
                            case 5:
                                tab.setText("内衣");
                                break;
                            case 6:
                                tab.setText("女装");
                                break;
                        }
                    }
                }

        );
        //使tablayout和viewpager2连接
        mediator.attach();

    }



    private void getViews() {
        //获取控件对象
        tab1 = findViewById(R.id.tab1);
        vp2 = findViewById(R.id.vp2);


    }

    private void initPages() {
        //初始化集合对象
        fragments = new ArrayList<>();
        fragments.add(new RecommendFragment());
        fragments.add(new InfantFragment());
        fragments.add(new StoreFragment());
        fragments.add(new BagFragment());
        fragments.add(new FoodFragment());
        fragments.add(new UnderwearFragment());
        fragments.add(new WomenwearFragment());


    }

}




